home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfrsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.6 KB  |  258 lines

  1. /* Copyright (C) 1998, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfrsd.c,v 1.4 2000/09/19 19:00:53 lpd Exp $ */
  20. /* ReusableStreamDecode filter support */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "gxiodev.h"
  24. #include "oper.h"
  25. #include "stream.h"
  26. #include "strimpl.h"
  27. #include "sfilter.h"        /* for SubFileDecode */
  28. #include "files.h"
  29. #include "idict.h"
  30. #include "idparam.h"
  31. #include "iname.h"
  32. #include "store.h"
  33.  
  34. /* ---------------- Reusable streams ---------------- */
  35.  
  36. /*
  37.  * The actual work of constructing the filter is done in PostScript code.
  38.  * The operators in this file are internal ones that handle the dirty work.
  39.  */
  40.  
  41. /* <dict|null> .rsdparams <filters> <decodeparms|null> */
  42. /* filters is always an array; decodeparms is always either an array */
  43. /* of the same length as filters, or null. */
  44. private int
  45. zrsdparams(i_ctx_t *i_ctx_p)
  46. {
  47.     os_ptr op = osp;
  48.     ref *pFilter;
  49.     ref *pDecodeParms;
  50.     int Intent;
  51.     bool AsyncRead;
  52.     ref empty_array, filter1_array, parms1_array;
  53.     uint i;
  54.     int code;
  55.  
  56.     make_empty_array(&empty_array, a_readonly);
  57.     if (dict_find_string(op, "Filter", &pFilter) > 0) {
  58.     if (!r_is_array(pFilter)) {
  59.         if (!r_has_type(pFilter, t_name))
  60.         return_error(e_typecheck);
  61.         make_array(&filter1_array, a_readonly, 1, pFilter);
  62.         pFilter = &filter1_array;
  63.     }
  64.     } else
  65.     pFilter = &empty_array;
  66.     /* If Filter is undefined, ignore DecodeParms. */
  67.     if (pFilter != &empty_array &&
  68.     dict_find_string(op, "DecodeParms", &pDecodeParms) > 0
  69.     ) {
  70.     if (pFilter == &filter1_array) {
  71.         make_array(&parms1_array, a_readonly, 1, pDecodeParms);
  72.         pDecodeParms = &parms1_array;
  73.     } else if (!r_is_array(pDecodeParms))
  74.         return_error(e_typecheck);
  75.     else if (r_size(pFilter) != r_size(pDecodeParms))
  76.         return_error(e_rangecheck);
  77.     } else
  78.     pDecodeParms = 0;
  79.     for (i = 0; i < r_size(pFilter); ++i) {
  80.     ref f, fname, dp;
  81.  
  82.     array_get(pFilter, (long)i, &f);
  83.     if (!r_has_type(&f, t_name))
  84.         return_error(e_typecheck);
  85.     name_string_ref(&f, &fname);
  86.     if (r_size(&fname) < 6 ||
  87.         memcmp(fname.value.bytes + r_size(&fname) - 6, "Decode", 6)
  88.         )
  89.         return_error(e_rangecheck);
  90.     if (pDecodeParms) {
  91.         array_get(pDecodeParms, (long)i, &dp);
  92.         if (!(r_has_type(&dp, t_dictionary) || r_has_type(&dp, t_null)))
  93.         return_error(e_typecheck);
  94.     }
  95.     }
  96.     if ((code = dict_int_param(op, "Intent", 0, 3, 0, &Intent)) < 0 ||
  97.     (code = dict_bool_param(op, "AsyncRead", false, &AsyncRead)) < 0
  98.     )
  99.     return code;
  100.     push(1);
  101.     op[-1] = *pFilter;
  102.     if (pDecodeParms)
  103.     *op = *pDecodeParms;
  104.     else
  105.     make_null(op);
  106.     return 0;
  107. }
  108.  
  109. /* <file|string> <CloseSource> .reusablestream <filter> */
  110. /*
  111.  * The file|string operand must be a "reusable source", either:
  112.  *      - A string or bytestring;
  113.  *      - A readable, positionable file stream;
  114.  *      - A readable string stream;
  115.  *      - A SubFileDecode filter with an empty EODString and a reusable
  116.  *      source.
  117.  * Reusable streams are also reusable sources, but they look just like
  118.  * ordinary file or string streams.
  119.  */
  120. private int make_rss(P8(i_ctx_t *i_ctx_p, os_ptr op, const byte * data,
  121.             uint size, int space, long offset, long length,
  122.             bool is_bytestring));
  123. private int make_rfs(P5(i_ctx_t *i_ctx_p, os_ptr op, stream *fs,
  124.             long offset, long length));
  125. private int
  126. zreusablestream(i_ctx_t *i_ctx_p)
  127. {
  128.     os_ptr op = osp;
  129.     os_ptr source_op = op - 1;
  130.     long length = max_long;
  131.     bool close_source;
  132.     int code;
  133.  
  134.     check_type(*op, t_boolean);
  135.     close_source = op->value.boolval;
  136.     if (r_has_type(source_op, t_string)) {
  137.     uint size = r_size(source_op);
  138.  
  139.     check_read(*source_op);
  140.     code = make_rss(i_ctx_p, source_op, source_op->value.const_bytes,
  141.             size, r_space(source_op), 0L, size, false);
  142.     } else if (r_has_type(source_op, t_astruct)) {
  143.     uint size = gs_object_size(imemory, source_op->value.pstruct);
  144.  
  145.     if (gs_object_type(imemory, source_op->value.pstruct) != &st_bytes)
  146.         return_error(e_rangecheck);
  147.     check_read(*source_op);
  148.     code = make_rss(i_ctx_p, source_op,
  149.             (const byte *)source_op->value.pstruct, size,
  150.             r_space(source_op), 0L, size, true);
  151.     } else {
  152.     long offset = 0;
  153.     stream *source;
  154.     stream *s;
  155.  
  156.     check_read_file(source, source_op);
  157.     s = source;
  158. rs:
  159.     if (s->cbuf_string.data != 0) {    /* string stream */
  160.         long pos = stell(s);
  161.         long avail = sbufavailable(s) + pos;
  162.  
  163.         offset += pos;
  164.         code = make_rss(i_ctx_p, source_op, s->cbuf_string.data,
  165.                 s->cbuf_string.size,
  166.                 imemory_space((const gs_ref_memory_t *)s->memory),
  167.                 offset, min(avail, length), false);
  168.     } else if (s->file != 0) { /* file stream */
  169.         if (~s->modes & (s_mode_read | s_mode_seek))
  170.         return_error(e_ioerror);
  171.         code = make_rfs(i_ctx_p, source_op, s, offset + stell(s), length);
  172.     } else if (s->state->template == &s_SFD_template) {
  173.         /* SubFileDecode filter */
  174.         const stream_SFD_state *const sfd_state =
  175.         (const stream_SFD_state *)s->state;
  176.  
  177.         if (sfd_state->eod.size != 0)
  178.         return_error(e_rangecheck);
  179.         offset += sfd_state->skip_count - sbufavailable(s);
  180.         if (sfd_state->count != 0) {
  181.         long left = max(sfd_state->count, 0) + sbufavailable(s);
  182.  
  183.         if (left < length)
  184.             length = left;
  185.         }
  186.         s = s->strm;
  187.         goto rs;
  188.     }
  189.     else            /* some other kind of stream */
  190.         return_error(e_rangecheck);
  191.     if (close_source) {
  192.         stream *rs = fptr(source_op);
  193.  
  194.         rs->strm = source;    /* only for close_source */
  195.         rs->close_strm = true;
  196.     }
  197.     }
  198.     if (code >= 0)
  199.     pop(1);
  200.     return code;
  201. }
  202.  
  203. /* Make a reusable string stream. */
  204. private int
  205. make_rss(i_ctx_t *i_ctx_p, os_ptr op, const byte * data, uint size,
  206.      int string_space, long offset, long length, bool is_bytestring)
  207. {
  208.     stream *s;
  209.     long left = min(length, size - offset);
  210.  
  211.     if (icurrent_space < string_space)
  212.     return_error(e_invalidaccess);
  213.     s = file_alloc_stream(imemory, "make_rss");
  214.     if (s == 0)
  215.     return_error(e_VMerror);
  216.     sread_string_reusable(s, data + offset, max(left, 0));
  217.     if (is_bytestring)
  218.     s->cbuf_string.data = 0;    /* byte array, not string */
  219.     make_stream_file(op, s, "r");
  220.     return 0;
  221. }
  222.  
  223. /* Make a reusable file stream. */
  224. private int
  225. make_rfs(i_ctx_t *i_ctx_p, os_ptr op, stream *fs, long offset, long length)
  226. {
  227.     gs_const_string fname;
  228.     stream *s;
  229.     int code;
  230.  
  231.     if (sfilename(fs, &fname) < 0)
  232.     return_error(e_ioerror);
  233.     if (fname.data[0] == '%')
  234.     return_error(e_invalidfileaccess); /* can't reopen */
  235.     /* Open the file again, to be independent of the source. */
  236.     code = file_open_stream((const char *)fname.data, fname.size, "r",
  237.                 fs->cbsize, &s, iodev_default->procs.fopen,
  238.                 imemory);
  239.     if (code < 0)
  240.     return code;
  241.     if (sread_subfile(s, offset, length) < 0) {
  242.     sclose(s);
  243.     return_error(e_ioerror);
  244.     }
  245.     s->close_at_eod = false;
  246.     make_stream_file(op, s, "r");
  247.     return 0;
  248. }
  249.  
  250. /* ---------------- Initialization procedure ---------------- */
  251.  
  252. const op_def zfrsd_op_defs[] =
  253. {
  254.     {"2.reusablestream", zreusablestream},
  255.     {"2.rsdparams", zrsdparams},
  256.     op_def_end(0)
  257. };
  258.